package com.example.hehecat.yuejian1.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.GameCollectContract;
import com.example.hehecat.yuejian1.mvp.model.bean.GameBean;
import com.example.hehecat.yuejian1.mvp.presenter.GameCollectPresenter;
import com.example.hehecat.yuejian1.ui.adapter.GameListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;

public class GameCollectFragment extends Fragment implements GameCollectContract.View {

    Unbinder unbinder;
    @BindView(R.id.game_collect)
    RecyclerView gameCollect;
    private GameCollectPresenter presenter;
    private GameListAdapter madapter;
    int user_id;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_collect, container, false);
        unbinder = ButterKnife.bind(this, view);
        user_id = MyStaticVariable.newInstance().getUserid();


        //创建布局管理
        final RecyclerView recyclerView = view.findViewById(R.id.game_collect);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyItemDecoration());

        //创建适配器
        madapter = new GameListAdapter(R.layout.item_list,getContext());
        madapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        madapter.setNotDoAnimationCount(2);

        //监听item点击
        madapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                moveToGameInfo(Objects.requireNonNull(madapter.getItem(position)).getGameid());
                Log.d(TAG,"onItemClick:" +madapter.getItem(position).getGamename());
            }
        });

        //给RecycleView设置适配器
        presenter = new GameCollectPresenter(this);
        presenter.updateAdapterData(user_id);
        recyclerView.setAdapter(madapter);
        return view;
    }

    public static GameCollectFragment newInstance(String name){
        GameCollectFragment fragment = new GameCollectFragment();
        Bundle args = new Bundle();
        args.putString("name",name);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void updateAdapterData(List<GameBean> data) {
        madapter.addData(data);
    }

    @Override
    public void moveToGameInfo(int game_id) {
        GameInfoFragment gameInfoFragment = GameInfoFragment.newInstance(game_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,gameInfoFragment,null)
                .commit();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.foodinfo_return)
    void gameCollectReturn(){
        MineFragment fragment = new MineFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,fragment,null)
                .commit();
    }
}
